<?php

class Poll
{

	/**
	 * getPoll
	 * returns the mysql results of the requested poll (either given ID or else latest)
	 * 
	 * @param int $id Poll of the requested poll (if null, latest will be returned)
	 * @return boolean|array Returns false if poll was not found, else mysql results will be returned
	 */
	function getPoll($id = null)
	{
		if(!is_null($id))
		{
			$pollQuery = mysql_query("SELECT * FROM `poll_vragen` WHERE `id`='" . $id . "'");
		}
		else
		{
			$pollQuery = mysql_query("SELECT * FROM `poll_vragen` ORDER BY `id` DESC LIMIT 1");
		}

		if(mysql_num_rows($pollQuery) != 1)
		{
			return false;
		}

		return mysql_fetch_assoc($pollQuery);
	}

	/**
	 * attemptVote
	 * attempts to enter vote for poll
	 * 
	 * @param int $userID The ID of the user
	 * @param int $answerID The ID of the answer
	 * @param int $pollID The ID of the poll
	 * @return null|boolean Returns true if success, false if answer doesn't exist or mysql query failed and, or null if the poll doesn't exist
	 */
	function attemptVote($userID, $answerID, $pollID = null)
	{
		#poll bestaat niet
		if(!($poll = $this->getPoll($pollID)))
		{
			die("Poll doesn't exist.");
			return null;
		}

		#antwoord bestaat niet
		if(empty($poll['antwoord_'  .  $answerID]))
		{
			die("Answer doesn't exist.");
			return false;
		}

		#alles bestaat
		return mysql_query("INSERT INTO `poll_antwoorden` (poll_id, user_id, antwoord) VALUES (" . $poll['id'] . ", " . $userID . ", " . $answerID . ")");
	}

	/**
	 * showPollResults
	 * prints the HTML result of the given poll ID
	 * 
	 * @param int $pollID The ID of the poll you want results from
	 * @return null Returns null if poll does not exist
	 */
	function showPollResults($pollID = null)
	{
		#poll bestaat niet
		if(!($poll = $this->getPoll($pollID)))
		{
			return null;
		}

		if(is_null($pollID))
		{
			$pollID = $poll['id'];
		}

		$resultsQuery = mysql_query("SELECT antwoord, COUNT(antwoord) AS aantal FROM `poll_antwoorden` WHERE `poll_id`=" . $pollID . " GROUP BY `antwoord`");
		$results = array();
		$totalvotes = 0;

		while($result = mysql_fetch_assoc($resultsQuery))
		{
			$results[$result['antwoord']] = $result['aantal'];
			$totalvotes += $result['aantal'];
		}

		# no /0 please
		$totalvotes = ($totalvotes > 0 ) ? $totalvotes : 1;

		echo '<li><strong>'. $poll['vraag'] . '</strong></li>';

		for($i = 1; $i <= 4; $i++)
		{
			if(!empty($poll['antwoord_' . $i]))
			{
				$votes = isset($results[$i]) ? $results[$i] : 0;
				$percentage = $votes / $totalvotes * 100;

				echo '<li>' . $poll['antwoord_' . $i] . ' (' . $votes . ' ' . (($votes != 1) ? 'stemmen' : 'stem') . ', ' . round($percentage) . '%)<div class="progress"><div class="bar" style="width: ' . $percentage . '%"></div></div></li>';
			}
		}
	}

	/**
	 * hasUserVoted
	 * returns boolean to show if the user has voted on the given poll
	 * 
	 * @param int $user_id The ID of the user
	 * @param int $pollid the ID of the poll
	 * @return boolean returns true if the user has voted, false if not
	 */
	function hasUserVoted($user_id, $pollid = null)
	{
		if(!is_null($pollid))
		{
			$result = mysql_num_rows(mysql_query("SELECT `antwoord` FROM `poll_antwoorden` WHERE user_id = " . $user_id . " AND poll_id=" . $poll_id));
		}
		else
		{
			$poll = $this->getPoll();
			$result = mysql_num_rows(mysql_query("SELECT `antwoord` FROM `poll_antwoorden` WHERE user_id = " . $user_id . " AND poll_id=" . $poll['id']));
		}

		return ($result > 0);
	}

	/**
	 * showLatestPoll
	 * shows the poll form for the latest poll
	 * 
	 * @param boolean $notLoggedIn Whether the user is logged in. If not, the submit button will be replaced with text.
	 */
	function showLatestPoll($notLoggedIn = false)
	{
		$poll = $this->getPoll();

		echo '<strong>' . $poll['vraag'] .'</strong><form id="form1" name="form1" method="post" action="">';

		for($i = 1; $i <= 4; $i++)
		{
			if(!empty($poll['antwoord_' . $i]))
			{
				echo '<li><label><input type="radio" name="poll_antwoord" value="' . $i . '" />' . $poll['antwoord_' . $i] . '</label></li>';
			}
		}
		if(!$notLoggedIn)
		{
			echo '<li><input name="stem" value="stem" class="submit" type="submit" /></li></form>';
		}
		else
		{
			echo '<li>Je moet ingelogd zijn als leerling om te kunnen stemmen.</li></form>';
		}
	}

	/**
	 * getPolls
	 * returns an array of all polls
	 * 
	 * @return int|resource Returns a resource if polls exist, 0 if not.
	 */
	function getPolls()
	{
		$pollQuery = mysql_query("SELECT * FROM `poll_vragen` ORDER BY `id` DESC");

		if(mysql_num_rows($pollQuery) > 0)
		{
			return $pollQuery;
		}
		else
		{
			return 0;
		}
	}
}